/**
 * 
 */
package com.usc.nearbyus;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * This is a class created to test if Wi-fi Logging is working as expected or not
 * @author pranav
 *
 */
public class WifiLoggingTest extends Activity implements OnClickListener {
	

	private TextView textStatus;
	private Button buttonScan;
	WifiManager wifi;
	private BroadcastReceiver receiver;
	private String TAG = "WifiLoggingTest";
	private Button buttonList;
	

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifitest);		
		
		// Setup UI
		textStatus = (TextView) findViewById(R.id.wifiTextStatus);
		buttonScan = (Button) findViewById(R.id.wifiButtonScan);
		buttonList = (Button) findViewById(R.id.wifiListButton);
		buttonScan.setOnClickListener(this);
		buttonList.setOnClickListener(this);

		// Setup WiFi
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

		// Get WiFi status
		WifiInfo info = wifi.getConnectionInfo();
		// textStatus.append("\n\nWiFi Status: " + info.toString());

		// List available networks
		/*List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
		for (WifiConfiguration config : configs) {
			textStatus.append("\n\n" + config.toString());
		}*/

		// Register Broadcast Receiver
		if (receiver == null){
			receiver = new WiFiScanReceiver(this);
		}

		registerReceiver(receiver, new IntentFilter(
				WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
		Log.d(TAG , "onCreate()");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop called");
		unregisterReceiver(receiver);
	}

	public void onClick(View view) {
		Toast.makeText(this, "On Click Clicked. Toast to that!!!",
				Toast.LENGTH_LONG).show();

		if (view.getId() == R.id.wifiButtonScan) {			
			Log.d(TAG, "onClick() wifi.startScan()");
			wifi.startScan();
		} else if (view.getId() == R.id.wifiListButton) {
			Log.d(TAG, "List Button");
			Intent intent = new Intent(this,WifiList.class);
			startActivity(intent);			
		}
		
		
	}	

}
